package org.echo.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.echo.storage.Storage;
import org.echo.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;

@Component
public class Spider {

    @Autowired
    private Storage storage;

    @Autowired
    private SpiderConfig spiderConfig;

    @PostConstruct
    public void start() throws Exception {
        this.one("187867");
    }

    private void one(String id) {

        String url = MessageFormat.format(spiderConfig.getUrl(), id);

        String json = null;

        try {
            json = HttpUtil.get(url);
//            Song song = JsonUtil.toObject(json, Song.class);
//            storage.download(song.getSource(), song.getName());

        } catch (Exception e) {
            System.out.println("歌曲信息获取发生异常" + e.getMessage());
        }

        try{
            this.parse(json);
        }catch(Exception e){
            System.out.println("JSON解析出现异常");
        }

    }

    private void parse(String json) {

        JSONObject root = JSONObject.parseObject(json);

        this.parseSong(root);
        this.parseUser(root);
        this.parseChannel(root);
        this.parseTags(root);
    }

    private void parseSong(JSONObject root) {
        String id = (String) root.getJSONObject("info").get("id");
        String source = (String) root.getJSONObject("info").get("source");
        String viewCount = (String) root.getJSONObject("info").get("view_count");
        Integer likeCount = (Integer) root.getJSONObject("info").get("like_count");
        String pic = (String) root.getJSONObject("info").get("pic");
        String createTime = (String) root.getJSONObject("info").get("create_time");
        String info = (String) root.getJSONObject("info").get("info");

        System.out.println(id);
        System.out.println(source);
        System.out.println(viewCount);
        System.out.println(likeCount);
        System.out.println(pic);
        System.out.println(createTime);
        System.out.println(info);
    }

    private void parseUser(JSONObject root) {
        String id = (String) root
                .getJSONObject("info")
                .getJSONObject("user")
                .get("id");
        String name = (String) root
                .getJSONObject("info")
                .getJSONObject("user")
                .get("name");

        System.out.println(id);
        System.out.println(name);
    }

    private void parseChannel(JSONObject root) {
        String id = (String) root
                .getJSONObject("info")
                .getJSONObject("channel")
                .get("id");
        String name = (String) root
                .getJSONObject("info")
                .getJSONObject("channel")
                .get("name");

        System.out.println(id);
        System.out.println(name);
    }

    private void parseTags(JSONObject root){
        JSONArray jsonArray = root.getJSONObject("info")
                .getJSONArray("tags");

        for (Object o : jsonArray) {
            JSONObject tag = (JSONObject) o;
            String id = (String) tag.get("id");
            String name = (String) tag.get("name");

            System.out.println(id);
            System.out.println(name);
        }
    }
}
